package com.kpx.app.wxapi;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.kpx.app.activity.ActivityOrderDetail;
import com.kpx.app.activity.ActivityOrderList;
import com.kpx.app.activity.ActivityPayList;
import com.kpx.app.activity.ActivityPayResult;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.kpx.app.R;
import com.kpx.app.utils.datautils.LogUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.thirdparty.wechat.utils.WechatConstants;
import com.kpx.app.wigdet.DialogSingleButton;


public class WXPayEntryActivity extends ActivityBase implements IWXAPIEventHandler {
    private static final String TAG = WXPayEntryActivity.class.getSimpleName();
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wechat_pay_result);
        if (!TextUtils.isEmpty(WechatConstants.APP_ID)) {
            api = WXAPIFactory.createWXAPI(this, WechatConstants.APP_ID);
            api.handleIntent(getIntent(), this);
        }
    }

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (null != intent && null != api) {
            setIntent(intent);
            api.handleIntent(intent, this);
        }
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        LogUtils.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                getOrderPayStatus();
                return;
            }
            if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                ToastUtil.getInstance().showToast(this, R.string.wechat_cancel_operation);
                finish();
                return;
            }
            DialogSingleButton dialogSingleButton = new DialogSingleButton(this, resp.errStr, "确定", new DialogSingleButton.OnButtonClickListener() {

                @Override
                public void onButtonClick() {
                    closeAllOpenedActivityNoMain();
                    ActivityOrderDetail.inType = 2;
                    IntentUtil.gotoActivityAndFinish(WXPayEntryActivity.this, ActivityOrderDetail.class);
                }
            });
            dialogSingleButton.setCancelable(false);
            dialogSingleButton.show();
        }
    }

//    @Override
//    public void onNetworkResponse(int requestFlag, NetworkResult networkResult) {
//        super.onNetworkResponse(requestFlag, networkResult);
//        int resultCode = networkResult.getCode();
//        if (resultCode == 0) {
//            switch (requestFlag) {
//                case HttpUtils.GET_ORDER_PAY_STAUTS:
//                    ToastUtil.getInstance().showToast(context, R.string.order_payment_success);
////                    sendOrderSuccessMessage();
//                    toOrderList();
//                    break;
//            }
//        } else {
//            ToastUtil.getInstance().showToast(context, Constants.ERROR_MAP.get(networkResult.getCode()));
//        }
//        dissmissLoadingDialog();
//    }

    /**
     * 查询订单支付状态
     */
    private void getOrderPayStatus() {
        if (0 != WechatConstants.ORDER_ID) {
            showLoadingDialog();
            MyHashMap map = new MyHashMap();
            map.put("orderId",WechatConstants.ORDER_ID+"");
            new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
                @Override
                public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                    dissmissLoadingDialog();
                    if (result.getCode() == 0) {
                        ToastUtil.getInstance().showToast(WXPayEntryActivity.this, R.string.order_payment_success);
                        toOrderList();
                    } else {
                        finish();
                        ToastUtil.getInstance().showToast(WXPayEntryActivity.this, result.getMsg());
                    }
                }

                @Override
                public void onRequestError(String url, int statusCode, String error) {
                    dissmissLoadingDialog();
                    finish();
                    ToastUtil.getInstance().showToast(WXPayEntryActivity.this, error);
                }

            }, null, null).POST(HttpDataReqUrl.URL_POST_GET_CHECK_PAY_RESULT, true, map);
        }
    }

    /**
     * 跳转到订单列表页
     */
    private void toOrderList() {
        closeAllOpenedActivityNoMain();
        IntentUtil.gotoActivityAndFinish(WXPayEntryActivity.this, ActivityPayResult.class);
    }

}